TITLE MASM Template						(main.asm)

; Description:  Thomas Lachin CSC208 assignment 3
;				Parsing memory simulating a PDP-8 Instruction
; Revision date:

INCLUDE Irvine32.inc



.data
PDP8Code WORD		0000111110000000b
remainderStorage DWORD 0


;Strings for output
;----OpCode
opCodePrompt BYTE "OpCode: ", 0
opCodeAND BYTE "AND", 0
opCodeTAD BYTE "TAD", 0
opCodeISZ BYTE "ISZ", 0
opCodeDCA BYTE "DCA", 0
opCodeJMS BYTE "JMS", 0
opCodeJMP BYTE "JMP", 0
opCodeIOT BYTE "IOT", 0
opCodeOPR BYTE "OPR", 0
;----Indirect Bit
indirectBitPrompt BYTE "Indirect Bit: ", 0
;----Page Bit
pageBitPrompt BYTE "Page Bit: ", 0
;----Address
addressPrompt BYTE "Address in Octal: ",0 

;Indirect bit comparison to get only single bit out of the WORD
indirectBitFind WORD 0000000100000000b

;Page bit comparsion get a single bit out of the WORD
pageBitFind WORD 0000000010000000b

.code
main PROC
	MOV eax, 0
	MOV edx, 0
	;MOV ax, PDP8Code
	
	CALL GetInstruction
	CALL GetIndirectBit
	CALL GetPageBit
	CALL GetAddress
	exit
main ENDP

;-------------------------------------------
GetInstruction PROC 
;
;Gets the instruction from the first 3 bits
;-------------------------------------------
	MOV ax, PDP8Code
	SHR ax, 9				;Shift 9 to get to the first 3 bits
							;Which contain the opCode
							
	MOV edx, OFFSET opCodePrompt			;Just neat output for console
	CALL WriteString
	MOV edx, 0
	
	;The following are if statements to output proper opCode instruction
	.IF ax ==  0
		MOV edx, OFFSET opCodeAND
		CALL WriteString
		CALL Crlf
	.ENDIF
	.IF ax ==  1
		MOV edx, OFFSET opCodeTAD
		CALL WriteString
		CALL Crlf
	.ENDIF
	.IF ax ==  2
		MOV edx, OFFSET opCodeISZ
		CALL WriteString
		CALL Crlf
	.ENDIF
	.IF ax ==  3
		MOV edx, OFFSET opCodeDCA
		CALL WriteString
		CALL Crlf
	.ENDIF
	.IF ax ==  4
		MOV edx, OFFSET opCodeJMS
		CALL WriteString
		CALL Crlf
	.ENDIF
	.IF ax ==  5
		MOV edx, OFFSET opCodeJMP
		CALL WriteString
		CALL Crlf
	.ENDIF
	.IF ax ==  6
		MOV edx, OFFSET opCodeIOT
		CALL WriteString
		CALL Crlf
	.ENDIF
	.IF ax ==  7
		MOV edx, OFFSET opCodeOPR
		CALL WriteString
		CALL Crlf
	.ENDIF
	
	MOV ax, PDP8Code					;reset instruction to its former status
	MOV edx, 0							;reset edx
	ret
GetInstruction ENDP

;-------------------------------------------
GetIndirectBit PROC 
;
;Gets the indirect bit from the 4th bit
;-------------------------------------------
	MOV eax, 0						;reset eax
	MOV ax, indirectBitFind			;set up tester WORD for instruction
	AND ax, PDP8Code				;AND it to see isolate the indirect bit
	
	MOV edx, OFFSET indirectBitPrompt			;Just neat output for console
	CALL WriteString
	MOV edx, 0
	.IF ax == 0
		CALL WriteDec
	.ELSE
		MOV ax, 1
		CALL WriteDec
	.ENDIF
	CALL Crlf
	
	MOV eax, 0
	MOV ax, PDP8Code
		
	ret
GetIndirectBit ENDP


;-------------------------------------------
GetPageBit PROC 
;
;Gets the page bit from the 5th bit
;-------------------------------------------
	MOV eax, 0						;reset eax
	MOV ax, pageBitFind				;set up tester WORD for instruction
	AND ax, PDP8Code				;AND it to see isolate the page bit
	
	MOV edx, OFFSET pageBitPrompt			;Just neat output for console
	CALL WriteString
	MOV edx, 0
	.IF ax == 0
		CALL WriteDec
	.ELSE
		MOV ax, 1
		CALL WriteDec
	.ENDIF
	CALL Crlf
	
	MOV eax, 0
	MOV ax, PDP8Code
		
	ret
GetPageBit ENDP

;-------------------------------------------
GetAddress PROC 
;
;Gets the address bits from the 6th bit to the 12th bit
;-------------------------------------------
	MOV eax, 0						;reset eax
	
	MOV edx, OFFSET addressPrompt			;Just neat output for console
	CALL WriteString
	MOV edx, 0
	CALL PrintAddressInOctal
	
	ret
GetAddress ENDP

;-------------------------------------------
PrintAddressInOctal PROC 
;
;Gets the address bits from the 6th bit to the 12th bit
;-------------------------------------------
	MOV eax, 0						;reset registers
	MOV ebx, 1						;Multiplier
	MOV edx, 0						;remainder in DIV
	MOV ecx, 0						;used to divide by 8 everytime
	MOV esi, 0						;where the octal is stored
	
	
	
	MOV eax, OFFSET PDP8COde
	MOV ecx, 8
	DIV ecx
	;Keep dividing and adding remainder to esi
	;	which ends up being the number in octal
	.WHILE eax != 0
		MOV remainderStorage, eax
		MOV eax, 0
		MOV eax, edx
		MUL ebx
		
		ADD esi, eax
		MOV eax, remainderStorage
		DIV ecx
		CALL MoveToNextNumberPlace
	.ENDW
	
	
	;Do the loop once more so the final step can be completed and the whole octal is set
	MOV remainderStorage, eax
	MOV eax, 0
	MOV eax, edx
	MUL ebx
	
	ADD esi, eax
	MOV eax, remainderStorage
	DIV ecx
	
	
	
	;Output results
	MOV eax, esi
	CALL WriteDec
	CALL Crlf
	
	
	ret
PrintAddressInOctal ENDP

;-------------------------------------------
MoveToNextNumberPlace PROC 
;
;Moves ebx to next number place
;-------------------------------------------
	.IF ebx == 1
		MOV ebx, 10
	.ELSEIF ebx == 10
		MOV ebx, 100
	.ELSEIF ebx == 100
		MOV ebx, 1000
	.ELSEIF ebx == 1000
		MOV ebx, 10000
	.ELSEIF ebx == 10000
		MOV ebx, 100000
	.ELSEIF ebx == 100000
		MOV ebx, 1000000
	.ELSEIF ebx == 1000000
		MOV ebx, 10000000
	.ELSEIF ebx == 10000000
		MOV ebx, 100000000
	.ENDIF
	
	ret
MoveToNextNumberPlace ENDP



END main





